package com.bwhx.eruler.controller.casdilly;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.SizeRule;
import com.bwhx.eruler.bean.hodo.HodoSizeRule;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.SizeRuleService;
import com.bwhx.eruler.util.Constant;

@Controller
@RequestMapping("/sizeRuler")
public class SizeRulerController extends BaseController {
	
	@Autowired
	SizeRuleService sizeRuleService;
	
	@ResponseBody
	@RequestMapping("/findBySizeRulerID.ashx")
	public HashMap<String,Object> findBySizeRulerID(HttpServletRequest request){
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		try {
			List<SizeRule> sr = sizeRuleService.findBySizeRuleID((String) request.getAttribute("typeSizeRuleID"));
			resultMap.put(Constant.DATA, sr);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}
		
		
		return resultMap;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	 public Map<String,Object> sizeRulerList(Integer page,Integer rows,SizeRule sizeRuler) {
		

		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,sizeRuler);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<SizeRule> list = sizeRuleService.list(p);
			Integer count = sizeRuleService.count(sizeRuler);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdate.ashx")
	public String saveOrUpdateFenchang(SizeRule sizeRule) {
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			
			if(sizeRule.getSizeRuleID().equals("-1")){
				 
				sizeRule.setCreatorID(sessionUser.getsId().toString());
				sizeRule.setCreateTime(new Date());
				SizeRule sr = sizeRuleService.findByName(sizeRule.getsRName());
				
				if(sr==null){
					sizeRuleService.insert(sizeRule);
				}else{
					return "conflict";  
				}
			
				
				
			}else{
				
				sizeRule.setModifierID (sessionUser.getsId().toString());
				sizeRule.setModifyTime(new Date());
				
				sizeRuleService.update(sizeRule);

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.list_all.ashx")
	 public List<SizeRule> listAll(Integer page,Integer rows,SizeRule sizeRuler) {

        Page p = new Page(0,0,sizeRuler);
		List<SizeRule> list = sizeRuleService.list(p);
		SizeRule sizeRule = new SizeRule();
		sizeRule.setsRName("无");
		sizeRule.setSizeRuleID("-1");
		list.add(0, sizeRule);
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping("/hodo_findBySizeRulerID.ashx")
	public HashMap<String,Object> hodoFindBySizeRulerID(HttpServletRequest request){
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		try {
			List<HodoSizeRule> sr = sizeRuleService.hodoFindBySizeRuleID((String) request.getAttribute("typeSizeRuleID"));
			resultMap.put(Constant.DATA, sr);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}
		
		
		return resultMap;
		
	}
	
	
	 
	

}
